package com.tianbo.elfii.client.common;

import java.io.*;
import java.sql.*;
import java.net.*;
import java.awt.*;
import java.util.*;
import java.util.Timer;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;
import javax.imageio.*;

import com.tianbo.elfii.client.common.ElfiiCommonUtil.FeedBackType;

/**
 * <pre>
 <b>类功能描述：</b>
 * 
 * <pre>
 *      完成 Config-网络 部分功能要求。
 * </pre>
 * 
 * @version 1.0.1
 * @author 高伟
 */

public class ElfiiCommonConfigNet {
	JPanel configPanel;
	JPanel itemPanel;

	int infoBgStartX0;
	int infoBgStartY0;

	int dataChoiceIndex;

	boolean isAboutMachineSelected = false;
	boolean isWifiSettingSelected = false;

	JFrame mainFrame;
	ElfiiCommonRunInfo runInfo;

	ItemButtonPanel wifiConnectingPanel;
	ItemButtonPanel wifiSettingPanel;
	ItemButtonPanel localNetPanel;

	// ItemButtonPanel httpProxyPanel;

	final int Id_WifiConnecting = 1;
	final int Id_WifiSetting = 2;
	final int Id_LocalNet = 3;
	final int Id_HttpProxy = 4;

	int selectedId;

	JDialog localNetWindow;

	// JDialog httpProxyWindow ;

	ElfiiCommonLoginKeyboard keyboard;

	JTextField ipAddressField;
	JTextField ipMaskField;
	JTextField ipRouterField;
	JTextField dns1Field;
	JTextField dns2Field;

	JCheckBox dynamicIP;
	ImageIcon dynamicIPSelectedIcon;
	ImageIcon dynamicIPUnSelectedIcon;
	
	JPanel mainPanel;
	
	Timer netStatTimer = null;
	
	
	public void setRunInfo(ElfiiCommonRunInfo runInfo) {
		this.runInfo = runInfo;
		this.mainFrame = runInfo.getMainFrame();
	}
	
	public void cancelTimer(){
		if(netStatTimer!=null){
			netStatTimer.cancel();
		}
	}

	public void dealBackButton() {
		cancelTimer();
		
		mainFrame.getContentPane().removeAll();
		ElfiiCommonConfig config = new ElfiiCommonConfig();
		config.setRunInfo(runInfo);
		JPanel p = config.createViewPanel();
		mainFrame.getContentPane().setLayout(new BorderLayout());
		mainFrame.getContentPane().add(p, BorderLayout.CENTER);
		mainFrame.validate();
	
	}
	
	LanStaticIpPane lanStaticIpPane = null;

	public void dealOkButton() {
		
		cancelTimer();

		if (selectedId == Id_LocalNet) {
			if(lanStaticIpPane==null)
				return;

			lanStaticIpPane.screenshot();
			lanStaticIpPane.backPanel = mainPanel;
			mainFrame.setContentPane(lanStaticIpPane);
			lanStaticIpPane.showDialog();
			mainFrame.validate();

		}


		if (selectedId == Id_WifiSetting) {

			mainFrame.getContentPane().removeAll();
			mainFrame.getContentPane().setLayout(new BorderLayout());
			ElfiiCommonConfigNetWifi configItem = new ElfiiCommonConfigNetWifi();
			configItem.setRunInfo(runInfo);

			mainFrame.getContentPane().add(configItem.createViewPanel(),
					BorderLayout.CENTER);
			mainFrame.validate();
		}
	}
	

	class DynamicIPItemListener implements ItemListener {

		@Override
		public void itemStateChanged(ItemEvent e) {
			// TODO Auto-generated method stub
			Object obj = e.getItem();
			if (obj.equals(dynamicIP)) {
				if (dynamicIP.isSelected()) {
					dynamicIP.setIcon(dynamicIPSelectedIcon);

					ipAddressField.setEditable(false);
					ipMaskField.setEditable(false);
					ipRouterField.setEditable(false);
					dns1Field.setEditable(false);
					dns2Field.setEditable(false);
					
					ipAddressField.setText("192.168.0.100");
					ipMaskField.setText("255.255.255.0");
					ipRouterField.setText("192.168.0.1");
					dns1Field.setText("");
					dns2Field.setText("");

				} else {
					dynamicIP.setIcon(dynamicIPUnSelectedIcon);
					
					ipAddressField.setEditable(true);
					ipMaskField.setEditable(true);
					ipRouterField.setEditable(true);
					dns1Field.setEditable(true);
					dns2Field.setEditable(true);

					ipAddressField.setText("0.0.0.0");
					ipMaskField.setText("255.255.255.0");
					ipRouterField.setText("0.0.0.0");
					dns1Field.setText("");
					dns2Field.setText("");
					
				}
			}
		}
	}

	class LocalNetWindowPanel extends JPanel {

		BufferedImage infoImage;
		BufferedImage bgImg;

		public LocalNetWindowPanel() {
			File f;
			try {
				f = new File("resource-common/images/config/icons/tb.png");
				infoImage = ImageIO.read(f);

				int x0 = (int) mainFrame.getLocationOnScreen().getX();
				int y0 = (int) mainFrame.getLocationOnScreen().getY();
				Robot rbt = new Robot();
				bgImg = rbt
						.createScreenCapture(new Rectangle(x0, y0, 1024, 768));

			} catch (Exception e) {
				System.out.print(e);
			}
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
					0.4f));
			g2.drawImage(bgImg, null, 0, 0);
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
					1.0f));

			int rectX0 = 287, rectY0 = 50;
			int rectWidth = 450, rectHeight = 350;

			g2.setColor(Color.white);
			g2.fillRoundRect(rectX0, rectY0, rectWidth, rectHeight, 10, 10);
			g2.setColor(Color.black);
			g2.fillRoundRect(rectX0 + 5, rectY0 + 5, rectWidth - 10,
					rectHeight - 10, 10, 10);
			g2.drawImage(infoImage, null, rectX0 + 30, rectY0 + 20);
			g2.setColor(Color.gray);
			g2.drawLine(rectX0 + 20, rectY0 + 85, rectX0 + rectWidth - 20,
					rectY0 + 85);
			g2.fillRect(rectX0 + 5, rectY0 + rectHeight - 55, rectWidth - 10,
					50);
			 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
			g2.setFont(new Font(ElfiiCommonConstant.Font_Name, Font.PLAIN, 30));
			g2.setColor(Color.white);
			g2.drawString("以太网IP设置", rectX0 + 125, rectY0 + 60);
			g2.setFont(new Font(ElfiiCommonConstant.Font_Name, Font.PLAIN, 15));
			g2.drawString("IP地址", rectX0 + 30, rectY0 + 120);
			g2.drawString("网络掩码", rectX0 + 30, rectY0 + 150);
			g2.drawString("网 关", rectX0 + 30, rectY0 + 180);
			g2.drawString("DNS1", rectX0 + 30, rectY0 + 210);
			g2.drawString("DNS2", rectX0 + 30, rectY0 + 240);

			//g2.setColor(Color.white);
			g2.setFont(new Font(ElfiiCommonConstant.Font_Name, Font.PLAIN, 12));
			g2.drawString("自动获取IP", rectX0 + 140, rectY0 + 275);

		}

		public Dimension getPreferredSize() {
			return (new Dimension(300, 100));
		}
	}

	public void createConfigPanel() {

		JButton settingsButton = new JButton("网络配置");
		settingsButton.setFont(new Font(ElfiiCommonConstant.Font_Name, Font.PLAIN, 15));
		settingsButton.setBackground(Color.gray);
		settingsButton.setForeground(Color.white);
		settingsButton.setBorderPainted(false);
		settingsButton.setFocusPainted(false);
		settingsButton.setHorizontalAlignment(SwingConstants.LEFT);
		settingsButton.setModel(new DefaultButtonModel() {
			public boolean isPressed() {
				return false;
			}
		});

		String wifiStatStr = "未开启";
		
		if(ElfiiCommonConfigUtil.curConnectedAp!=null){
			wifiStatStr = "已连接至 "+ElfiiCommonConfigUtil.curConnectedAp.getSSID();
		}
		
		wifiConnectingPanel = new ItemButtonPanel("Wi-Fi", "未开启");
		wifiConnectingPanel.setFocusable(true);
		wifiConnectingPanel.setBackground(Color.black);
		wifiConnectingPanel.setSwitchStat(true);
		wifiConnectingPanel.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if(e.getPoint().x>900){
					setSelectButton(Id_WifiConnecting);
					ElfiiCommonConfigUtil.isWlanOpened = !ElfiiCommonConfigUtil.isWlanOpened;
					ElfiiCommonConfigUtil.setWlanOpened(ElfiiCommonConfigUtil.isWlanOpened);
				}
			}
		});

		wifiSettingPanel = new ItemButtonPanel("Wi-Fi设置", "设置和管理无线接入点");
		wifiSettingPanel.setFocusable(true);
		wifiSettingPanel.setBackground(Color.black);
		wifiSettingPanel.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				setSelectButton(Id_WifiSetting);
			}
		});
		
		
		String localNetStr2 = "";
		
		if(ElfiiCommonConfigUtil.getLanConnectedStat()){
			localNetStr2 = "已连接";		
		}
		else{
			localNetStr2 = "未连接";
		}

		localNetPanel = new ItemButtonPanel("以太网设置", localNetStr2);
		localNetPanel.setFocusable(true);
		localNetPanel.setBackground(Color.black);
		localNetPanel.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				setSelectButton(Id_LocalNet);
			}
		});

		netStatTimer = new Timer();
		
		netStatTimer.schedule(new TimerTask(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				if(ElfiiCommonConfigUtil.getLanConnectedStat()){
					localNetPanel.str2 = "已连接";
					
					if(ElfiiCommonConfigUtil.checkNetConnected(10, 3)>0){
						localNetPanel.str2 = "已连接(无服务器访问)";
					}			
				}
				else{
					localNetPanel.str2 = "未连接";
				}
				
				if(localNetPanel!=null){
					localNetPanel.repaint();
				}
			}
			
		}, 2000,2000);
	

		configPanel = new JPanel();
		configPanel.setBackground(Color.black);
		configPanel.setLayout(null);

		configPanel.add(settingsButton);
		settingsButton.setBounds(0, 10, 1024, 25);

		configPanel.add(wifiConnectingPanel);
		wifiConnectingPanel.setBounds(0, 45, 1024, 75);

		configPanel.add(wifiSettingPanel);
		wifiSettingPanel.setBounds(0, 120, 1024, 75);
		configPanel.add(localNetPanel);
		localNetPanel.setBounds(0, 195, 1024, 75);

		wifiConnectingPanel.setSelected(!ElfiiCommonConfigUtil.isWlanOpened);
		
		if(wifiConnectingPanel.isSelected){
			wifiConnectingPanel.setSelected(false);
			wifiSettingPanel.setDefaultColor(Color.gray);
		}
		else{
			wifiConnectingPanel.setSelected(true);
			wifiSettingPanel.setDefaultColor(Color.white);
		}
		
//		setSelectButton(Id_WifiConnecting);
//		
		wifiConnectingPanel.repaint();
		
	}

	public void setSelectButton(int selectedId) {
		
    	if(this.selectedId==selectedId && selectedId != Id_WifiConnecting){
    		return;
    	}
		
		this.selectedId = selectedId;
//		wifiConnectingPanel.setSelected(false);
		wifiSettingPanel.setSelected(false);
		localNetPanel.setSelected(false);
		// httpProxyPanel.setSelected(false);
		if (selectedId == Id_WifiConnecting){
			if(wifiConnectingPanel.isSelected){
				wifiConnectingPanel.setSelected(false);
				wifiSettingPanel.setDefaultColor(Color.gray);
			}
			else{
				wifiConnectingPanel.setSelected(true);
				wifiSettingPanel.setDefaultColor(Color.white);
			}
		}
		if (selectedId == Id_WifiSetting && wifiConnectingPanel.isSelected)
			wifiSettingPanel.setSelected(true);
		if (selectedId == Id_LocalNet)
			localNetPanel.setSelected(true);
		// if (selectedId==Id_HttpProxy)
		// httpProxyPanel.setSelected(true);

		ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
	}


	class ItemButtonPanel extends JPanel {
		BufferedImage selectedBgImage;

		BufferedImage lineImage;

		BufferedImage switchOnImage;

		BufferedImage switchOffImage;

		BufferedImage img2;

		BufferedImage gaoImg;

		String str1;
		String str2;
		boolean isSelected;
		boolean isSwitch;
		
		Color defaultColor;

		public ItemButtonPanel(String str1, String str2) {
			this.str1 = str1;
			this.str2 = str2;
			File f = null;
			try {
				f = new File("resource-common/images/config/icons/tc_butbg.png");
				selectedBgImage = ImageIO.read(f);
				f = new File("resource-common/images/config/icons/tb_bg.png");
				img2 = ImageIO.read(f);
				f = new File("resource-common/images/config/icons/line.png");
				lineImage = ImageIO.read(f);

				f = new File("resource-common/images/config/icons/switchOn.png");
				switchOnImage = ImageIO.read(f);
				f = new File(
						"resource-common/images/config/icons/switchOff.png");
				switchOffImage = ImageIO.read(f);

				isSwitch = false;
				defaultColor = Color.white;

			} catch (Exception e) {
				System.out.print(e + "   " + f);
			}
		}

		public void setSwitchStat(boolean isSwitchOn) {
			isSwitch = true;
			if (isSwitchOn) {
				this.setSelected(true);
			} else {
				this.setSelected(false);
			}
		}

		public void setSelected(boolean isSelected) {
			this.isSelected = isSelected;
			repaint();
		}
		
		public void setDefaultColor(Color newColor){
			defaultColor = newColor;
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);

			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			g2.setColor(Color.black);
			g2.fillRect(0, 0, 1024, 75);
			if (isSelected) {
				if (!isSwitch) {
					g2.drawImage(selectedBgImage, null, 0, 0);
					// g2.drawImage(img2,null,0,0);
					// g2.drawImage(selectedImage,null,10,0);
					g2.setColor(ElfiiCommonConstant.Elfii_Blue);
				}
				else{
					g2.drawImage(switchOnImage, null, 0, 0);
					g2.setColor(defaultColor);
					str2 = "已开启";
				}
			} else {
				if(isSwitch){
					g2.drawImage(switchOffImage, null, 0, 0);
					str2 = "未开启";
				}
				g2.setColor(defaultColor);
				
			}

			g2.setFont(new Font(ElfiiCommonConstant.Font_Name, Font.PLAIN, 20));
			g2.drawString(str1, 20, 28);
			g2.setFont(new Font(ElfiiCommonConstant.Font_Name, Font.PLAIN, 14));
			g2.drawString(str2, 20, 50);
			g2.drawImage(lineImage, null, 0, 74);
		}

		public Dimension getPreferredSize() {
			return (new Dimension(1024, 75));
		}
	}

	public JPanel createViewPanel() {
		mainPanel = new JPanel(new BorderLayout());
		JButton okButton = new JButton();
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dealOkButton();
				ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
			}
		});

		JButton backButton = new JButton();
		backButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dealBackButton();
				ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
			}
		});

		JButton homeButton = runInfo.createHomeButton();
		JPanel topPanel = ElfiiCommonUtil.createTopPanel("系统设置", backButton,
				homeButton);
		JPanel bottomPanel = ElfiiCommonUtil
				.createBottomPanelLevelSelect(okButton);
		createConfigPanel();
		mainPanel.setLayout(null);
		mainPanel.setBackground(Color.black);
		mainPanel.add(topPanel);
		topPanel.setBounds(0, 0, 1024, 100);
		mainPanel.add(bottomPanel);
		bottomPanel.setBounds(0, 668, 1024, 100);
		mainPanel.add(configPanel);
		configPanel.setBounds(0, 101, 1024, 560);
		
		Timer timer = new Timer();
		
		timer.schedule(new TimerTask(){
			public void run() {
				// TODO Auto-generated method stub
				initNextPanel();
			}
		}, 100);
		

		return mainPanel;
	}
	
	public void initNextPanel(){
		lanStaticIpPane = new LanStaticIpPane(this);
	}
	
}
